package com.koron.standard.deviceBrand.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koron.standard.bean.base.BaseBean;
import com.koron.standard.deviceBrand.bean.po.BrandStock;
import com.koron.standard.deviceBrand.mapper.BrandStockMapper;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 设备品牌库(BrandStock)表服务实现类
 *
 * @author jzp
 * @since 2022-01-13
 */
@Service
public class BrandStockService extends ServiceImpl<BrandStockMapper, BrandStock> {

    /**
     * 名称不允许重复
     */
    public int countName(BrandStock brandStock) {
        LambdaQueryWrapper<BrandStock> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BrandStock::getName, brandStock.getName());
        return count(wrapper);
    }

    /**
     * 查询所有的品牌简称
     */
    public Set<String> listAllName() {
        LambdaQueryWrapper<BrandStock> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(BrandStock::getName);
        List<BrandStock> list = list(wrapper);
        if (list == null) {
            return new HashSet<>();
        }
        return list.stream().map(BrandStock::getName).collect(Collectors.toSet());
    }

    /**
     * 筛选出不重复的品牌库
     */
    public List<BrandStock> dressingByDuplicated(List<BrandStock> source) {
        if (CollUtil.isEmpty(source)) {
            return null;
        }
        LambdaQueryWrapper<BrandStock> wrapper = new LambdaQueryWrapper<>();
        wrapper.notIn(BrandStock::getName, source.stream().map(BaseBean::getId).collect(Collectors.toList()));
        return list(wrapper);
    }

}
